﻿using UnityEngine;
using System.Collections;

public class ShaneAttackScript : MonoBehaviour {

	public float ThrustLength = 1.5f;
	public float thrustSpeed = 7.0f;
	public ParticleSystem boostParticles;

	public Transform groundcheck;
	bool grounded = false;
	public float jumpHeight = 300.0f;
	public float slamForce = 300.0f;
	bool slamming = false;
	public float maxSlamDistance = 3.0f;
	public float slamDamage = 20.0f;
	public AudioSource slamSound;

	public GameObject slamParticle;
	public float ramForceRegular = 8.0f;
	public float ramForceThrusting = 15.0f;

	ShayneMovementScript myMoveScript;
	Inventory myInventoryScript;

	KeyCode thrustKey = KeyCode.Space;
	KeyCode slamKey = KeyCode.LeftShift;

	// Use this for initialization
	void Start ()
	{
		myMoveScript = this.GetComponent<ShayneMovementScript> ();
		myInventoryScript = this.GetComponent<Inventory> ();
	}
	
	// Update is called once per frame
	void Update ()
	{
		grounded = Physics.Linecast (transform.position, groundcheck.position);

		if(slamming && grounded)
			SlamHit ();


		if(Input.GetKeyDown(thrustKey))
		{
			Thrust ();
		}
		if(Input.GetKeyDown(slamKey) && grounded)
		{
			SlamStart ();
		}

	}


	void Thrust()
	{
		if(myInventoryScript.UseCharge())
		{
			boostParticles.Play ();
			myMoveScript.SetSpeed(thrustSpeed, ThrustLength);
		}
	}


	void SlamStart()
	{
		rigidbody.AddForce(Vector3.up * jumpHeight);
		Invoke ("SlamDown", 1.0f);
	}

	void SlamDown()
	{
		//rigidbody.velocity = Vector3.zero;
		rigidbody.AddForce (Vector3.down * slamForce);
		slamming = true;
	}

	void SlamHit()
	{
		slamSound.Play ();

		if(slamParticle)
		{
			slamParticle.particleSystem.Play ();
			slamming = false;
		}
		else
			Debug.Log("ERROR: Partical System disapeared???");

		foreach(GameObject enemy in GameObject.FindGameObjectsWithTag ("Enemy"))
		{
			float dist = Vector3.Distance (transform.position, enemy.transform.position);
			if(dist < maxSlamDistance)
			{
				Hittable h = enemy.GetComponent (typeof(Hittable)) as Hittable;
				if (h != null) 
				{
						h.Hit(slamDamage);
				}
			}
		}
	}


	void ShootNearbyEnemies()
	{

	}

	void ShootRocket()
	{

	}
}
